import { isEmpty, isArray } from '@/utils/types'
import i18n from '@/i18n/i18n'

// 5 为空 6 不为空
export const isEmptyCondition = function(type) {
  return [5, 6].includes(type)
}

/**
 * 获取上传数据组
 */
export function getFilterPostData(list) {
  const obj = []
  list.forEach((o) => {
    let dataValues = []
    if (o.formType == 'datetime' || o.formType == 'date') {
      if (o.type === 14) {
        if (!isEmpty(o.timeType)) {
          dataValues = [o.timeType]
        } else {
          dataValues = o.range || []
        }
      } else if (o.value) {
        dataValues = isArray(o.value) ? o.value : [o.value]
      }
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        formType: o.formType,
        name: o.fieldName,
        type: o.type,
        values: dataValues
      })
    } else if (
      o.formType == 'number' ||
      o.formType == 'floatnumber' ||
      o.formType == 'percent'
    ) {
      if (o.type === 14) {
        dataValues = [isEmpty(o.min) ? 0 : o.min, isEmpty(o.max) ? 0 : o.max]
      } else if (!isEmpty(o.value)) {
        dataValues = [o.value]
      }
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        formType: o.formType,
        name: o.fieldName,
        type: o.type,
        values: dataValues
      })
    } else if (['flowGroup', 'business_cause'].includes(o.formType)) {
      // 阶段组 和 阶段 依据name传值
      if (o.flowName) {
        obj.push({
          formType: o.formType,
          name: 'flowName',
          type: 1,
          values: [o.flowName]
        })
      }
      if (o.settingName) {
        obj.push({
          formType: o.formType,
          name: 'settingName',
          type: 1,
          values: [o.settingName]
        })
      }
    } else if (o.formType == 'user' || o.formType == 'single_user') {
      dataValues = o.value
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType == 'position') {
      dataValues = o.value
        .filter((item) => !isEmpty(item.name))
        .map((item) => JSON.stringify(item))
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType == 'structure') {
      dataValues = o.value
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType == 'checkbox') {
      // 回款状态 formType 为 checkbox
      if (o.fieldName === 'receivedStatus' && !isArray(o.value)) {
        dataValues =
          o.value === '' || o.value === null || o.value === undefined
            ? []
            : [o.value]
      } else {
        dataValues = isArray(o.value) ? o.value : []
      }

      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType == 'category') {
      dataValues =
        isArray(o.value) && o.value.length > 0
          ? [o.value[o.value.length - 1]]
          : []
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: 1,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType === 'select') {
      dataValues = o.value || []
      if (typeof dataValues === 'string') {
        dataValues = [dataValues]
      }
      if (
        getFilterSettingValueType(o.setting) != 'string' &&
        (o.value || o.value === 0)
      ) {
        dataValues = [o.value]
      }
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (['field_tag', 'field_attention'].includes(o.formType)) {
      dataValues = o.value || []
      if (!isArray(dataValues)) {
        dataValues = [dataValues]
      }
      if (isEmptyCondition(o.type)) dataValues = []
      obj.push({
        type: o.type,
        values: dataValues,
        formType: o.formType,
        name: o.fieldName
      })
    } else if (o.formType == 'map_address') {
      const value = []
      for (var key in o.address) {
        const addValue = o.address[key]
        if (addValue) {
          value.push(addValue)
        }
      }
      obj.push({
        values: [value.join(',')],
        type: 3,
        formType: o.formType,
        name: o.fieldName
      })
    } else {
      let values = []
      if (typeof o.value === 'string') {
        const temp = o.value.replace(/；/g, ';')
        values = temp.split(';').filter((item) => item !== '' && item !== null)
      } else {
        values = [o.value]
      }
      if (isEmptyCondition(o.type)) values = []
      obj.push({
        type: o.type,
        values: values,
        formType: o.formType,
        name: o.fieldName
      })
    }
  })

  return obj
}

/**
 * 获取setting数据类型
 */
function getFilterSettingValueType(setting) {
  if (setting && setting.length > 0) {
    const value = setting[0]
    return typeof value
  }
  return []
}

// 时间类型快件筛选值
export const timeTypeShortcuts = [
  { text: i18n.t('components.FilterForm.104aba48ab66f154806ecf0459fb0674'), value: 'year' },
  { text: i18n.t('components.FilterForm.59e2c990221de822a40dcda382013553'), value: 'lastYear' },
  { text: i18n.t('components.FilterForm.829b37cf96bf1a65fff2e831874062d7'), value: 'nextYear' },
  { text: i18n.t('components.FilterForm.7f9c1d2da59603857e050f795d37bf36'), value: 'firstHalfYear' },
  { text: i18n.t('components.FilterForm.0f13a987f3327c474241e23763df3e58'), value: 'nextHalfYear' },
  { text: i18n.t('components.FilterForm.6fb0989b59d0fd1d47ea1e92a4017dc0'), value: 'quarter' },
  { text: i18n.t('components.FilterForm.c5c28c9013e177f6a868cad9c1168398'), value: 'lastQuarter' },
  { text: i18n.t('components.FilterForm.43b169e1622c435c6d8e59049eec88de'), value: 'nextQuarter' },
  { text: i18n.t('components.FilterForm.a7a506cd8b2d09e80f6543f23ad311d4'), value: 'month' },
  { text: i18n.t('components.FilterForm.4c0b4b5b2f14c110933dedffe38e26c0'), value: 'lastMonth' },
  { text: i18n.t('components.FilterForm.d0617034826aa82eaf968b8c96c49ccf'), value: 'nextMonth' },
  { text: i18n.t('components.FilterForm.9be932862a113f7f00b6f6fd749be63e'), value: 'week' },
  { text: i18n.t('components.FilterForm.ba4329333d1a672bd09696aa784e66a7'), value: 'lastWeek' },
  { text: i18n.t('components.FilterForm.d08879870988e722abe588e56210c05e'), value: 'nextWeek' },
  { text: i18n.t('components.FilterForm.d8340cac1abbb3b735640038e21d3b2c'), value: 'today' },
  { text: i18n.t('components.FilterForm.2d890c8447216edca0310f8791300566'), value: 'yesterday' },
  { text: i18n.t('components.FilterForm.818246f6c11b04d92c21642095755235'), value: 'tomorrow' },
  { text: i18n.t('components.FilterForm.f7f2882b1e9e622ea7cbcbbbe88a2ccb'), value: 'previous7day' },
  { text: i18n.t('components.FilterForm.b0b65b513fb0ebbe64133fbe98740df2'), value: 'previous30day' },
  { text: i18n.t('components.FilterForm.324eb6443854993c55c8836c3aa8811e'), value: 'future7day' },
  { text: i18n.t('components.FilterForm.b524067677bdf91861cb0ec1f54e25b1'), value: 'future30day' }
]
